package com.easy.annex.controller;

import com.easy.annex.FileModel;
import com.easy.annex.service.IFileService;
import com.platform.base.common.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Optional;

@RestController
@Api(tags = "小文件")
@RequestMapping("/file")
public class FileController {

    @Resource
    IFileService mIFileService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "api/v1/page")
    @ResponseBody
    public Result<List<FileModel>> listFilesByPage(@RequestParam int page, @RequestParam int size){
        return new Result<>(mIFileService.listFilesByPage(page, size));
    }

    @ApiOperation(value = "上传单个小文件")
    @PostMapping("api/v1/upload")
    public Result<FileModel> uploadFile(@ApiParam(name = "file", value = "附件", required = true) MultipartFile file, HttpServletRequest request) {
        FileModel fileModel = mIFileService.saveFile(file, request);
        return new Result<>(fileModel);
    }

    @ApiOperation(value = "上传单个小文件")
    @PostMapping("api/v1/upload/simple")
    public Result<String> uploadFileSimple(@ApiParam(name = "file", value = "附件", required = true) MultipartFile file, HttpServletRequest request) {
        FileModel fileModel = mIFileService.saveFile(file, request);
        return new Result<>(fileModel == null ? "" : fileModel.getId());
    }

    @ApiOperation(value = "显示文件")
    @GetMapping(value = "api/v1/view")
    @ResponseBody
    public ResponseEntity<Object> viewFile(@RequestParam @NotBlank String fileId) {

        Optional<FileModel> file = mIFileService.getFileById(fileId);

        if (file.isPresent()) {
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "fileName=\"" + file.get().getName() + "\"")
                    .header(HttpHeaders.CONTENT_TYPE, file.get().getContentType())
                    .header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "").header("Connection", "close")
                    .body(file.get().getContent().getData());
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
        }
    }

    @ApiOperation(value = "文件详情")
    @GetMapping(value = "api/v1/detail")
    @ResponseBody
    public ResponseEntity<Object> viewFileDetail(@RequestParam @NotBlank String fileId) throws UnsupportedEncodingException {

        Optional<FileModel> file = mIFileService.getFileById(fileId);

        if (file.isPresent()) {
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=" + new String(file.get().getName().getBytes("utf-8"),"ISO-8859-1"))
                    .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
                    .header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "").header("Connection", "close")
                    .body(file.get().getContent().getData());
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
        }
    }

    @ApiOperation(value = "删除单个小件")
    @DeleteMapping(value = "api/v1/remove")
    public Result<Boolean> deleteFile(@RequestParam @NotBlank String fileId) {
       mIFileService.removeFile(fileId);
        return new Result<>();
    }
}